package leetcode.code0874;

import java.awt.Point;
import java.util.HashSet;
import java.util.Set;

import leetcode.IDebug;
import leetcode.helper.H;


public class Solution2 implements IDebug {

	int[] xx = new int[] { 0, 1, 0, -1 };
	int[] yy = new int[] { 1, 0, -1, 0 };

	Set<Point> obs;

	public int robotSim(int[] commands, int[][] obstacles) {
		int[] pos = new int[2];
		obs = new HashSet<>();
		for (int i = 0; i < obstacles.length; i++) {
			obs.add(new Point(obstacles[i][0], obstacles[i][1]));
		}
		int ans = 0, d = 0;// 答案, 方向初始0
		for (int i = 0; i < commands.length; i++) {
			d = this.command(pos, d, commands[i]);// pos 当前位置，d当前方向索引，command当前命令
			ans = Math.max(ans, pos[0] * pos[0] + pos[1] * pos[1]);
		}
		return ans;
	}

	// pos 当前位置， d当前方向索引 ， command当前命令
	private int command(int[] pos, int d, int command) {
		if (command == -2) {// 左转
			return (d + 3) % 4; // d + 4 - 1
		} else if (command == -1) {// 右转
			return (d + 1) % 4;
		} else {
			for (int i = 1; i <= command && !obs.contains(new Point(pos[0] + xx[d], pos[1] + yy[d])); i++) {
				pos[0] += xx[d];
				pos[1] += yy[d];
			}
		}
		return d;
	}

	@Override
	public void debug4() {
		H.compare(65, this.robotSim(H.str2array3("[4,-1,4,-2,4]"), H.str2array1("[[2,4]]")));
		H.compare(36, this.robotSim(H.str2array3("[6,-1,-1,6]"), H.str2array1("[]")));
		H.compare(25, this.robotSim(H.str2array3("[4,-1,3]"), H.str2array1("[]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution2 so = new Solution2();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
